package com.cangshukj.web.controller.junong;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.cangshukj.junong.domain.LandPics;
import com.cangshukj.junong.service.ILandPicsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.cangshukj.common.annotation.Log;
import com.cangshukj.common.core.controller.BaseController;
import com.cangshukj.common.core.domain.AjaxResult;
import com.cangshukj.common.enums.BusinessType;
import com.cangshukj.junong.domain.LandNews;
import com.cangshukj.junong.service.ILandNewsService;
import com.cangshukj.common.utils.poi.ExcelUtil;
import com.cangshukj.common.core.page.TableDataInfo;

/**
 * 新闻信息Controller
 * 
 * @author wangjie
 * @date 2025-04-01
 */
@Api(value = "/land/news",description = "新闻信息")
@RestController
@RequestMapping("/land/news")
public class LandNewsController extends BaseController
{
    @Autowired
    private ILandNewsService landNewsService;
    @Autowired
    private ILandPicsService landPicsService;

    /**
     * 查询新闻信息列表
     */
    @ApiOperation("")
    @PreAuthorize("@ss.hasPermi('land:news:list')")
    @GetMapping("/list")
    public TableDataInfo list(LandNews landNews)
    {
        startPage();
        List<LandNews> list = landNewsService.selectLandNewsList(landNews);
        clearPage();
        return getDataTable(list);
    }


    /**
     * 获取新闻信息详细信息
     */
    @ApiOperation("获取新闻信息详细信息")
    @PreAuthorize("@ss.hasPermi('land:news:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        LandNews landNews = landNewsService.selectLandNewsById(id);
        String dataid = landNews.getId();
        String tablename = "land_news";
        List<LandPics>  pics = landPicsService.selectLandPicsByFile(dataid,tablename);
        landNews.setLandNews(pics);
        return AjaxResult.success(landNews);
    }

    /**
     * 新增新闻信息
     */
    @ApiOperation("新增新闻信息")
    @PreAuthorize("@ss.hasPermi('land:news:add')")
    @Log(title = "新闻信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LandNews landNews)
    {
        landNews.setCreateBy(getUsername());
        landNews.setCreateTime(new Date());
        landNewsService.insertLandNews(landNews);
        //获取多图片集合
        List<LandPics> news = landNews.getLandNews();
        for (LandPics pic:news){
            LandPics p = new LandPics();
            p.setCreateTime(new Date());
            p.setCreateBy(getUsername());
            p.setDataid(landNews.getId());
            p.setTablename("land_news");
            p.setFieldname("news_imgs");
            p.setFileName(pic.getFileName());
            p.setFilePath(pic.getFilePath());
            landPicsService.insertLandPics(p);
        }
        return AjaxResult.success();
    }

    /**
     * 修改新闻信息
     */
    @ApiOperation("修改新闻信息")
    @PreAuthorize("@ss.hasPermi('land:news:edit')")
    @Log(title = "新闻信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LandNews landNews)
    {
        String dataid = landNews.getId();
        List<LandPics> news = landNews.getLandNews();
        if (news.size() > 0){
            landPicsService.deleteLandPicsByDataId(dataid);
            for (LandPics pic:news){
                LandPics p = new LandPics();
                p.setCreateTime(new Date());
                p.setCreateBy(getUsername());
                p.setDataid(landNews.getId());
                p.setTablename("land_news");
                p.setFieldname("news_imgs");
                p.setFileName(pic.getFileName());
                p.setFilePath(pic.getFilePath());
                landPicsService.insertLandPics(p);
            }
        }
        landNews.setUpdateBy(getUsername());
        landNews.setUpdateTime(new Date());
        return toAjax(landNewsService.updateLandNews(landNews));
    }

    /**
     * 删除新闻信息
     */
    @ApiOperation("删除新闻信息")
    @PreAuthorize("@ss.hasPermi('land:news:remove')")
    @Log(title = "新闻信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(landNewsService.deleteLandNewsByIds(ids));
    }
}
